package com.zygh.lz.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.lz.dao.DutyModelMapper;
import com.zygh.lz.dao.DutyScheduleMapper;
import com.zygh.lz.dao.XareaModeMapper;
import com.zygh.lz.entity.XareaMode;
import com.zygh.lz.entity.duty.DutyModel;
import com.zygh.lz.entity.duty.DutySchedule;
import com.zygh.lz.service.DutyScheduleService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lee
 */
@Service
public class DutyScheduleServiceImpl implements DutyScheduleService {
    @Resource
    private DutyScheduleMapper dutyScheduleMapper;
    @Resource
    private DutyModelMapper dutyModelMapper;

    @Override
    public ResultBean find(DutySchedule dutySchedule) {
        PageHelper.startPage(dutySchedule.getPageNum(), dutySchedule.getPageSize());
        List<DutySchedule> dutySchedules = dutyScheduleMapper.selectList(Wrappers.<DutySchedule>lambdaQuery()
                .like(StrUtil.isNotBlank(dutySchedule.getSignComment()), DutySchedule::getSignout, dutySchedule.getSignComment()));

        List<Integer> modelCodes = dutySchedules.stream().map(DutySchedule::getModeCode).collect(Collectors.toList());
        List<DutyModel> dutyModels = dutyModelMapper.selectList(Wrappers.<DutyModel>lambdaQuery()
                .in(DutyModel::getCode, modelCodes));

        dutySchedules.forEach(x -> {
            dutyModels.forEach(y -> {
                if (Objects.equals(x.getModeCode(), y.getCode())) {
                    x.setModeName(y.getName());
                }
            });
        });

        PageInfo<DutySchedule> pageInfo = new PageInfo<>(dutySchedules);
        return ResultUtil.setOK("查询成功", pageInfo);
    }

    @Override
    public ResultBean add(DutySchedule dutySchedule) {
        String signout = dutySchedule.getSignout();
        DateTime parse = DateUtil.parse(signout);

        int hour = parse.getField(DateField.HOUR_OF_DAY);
        int minute = parse.getField(DateField.MINUTE);
        int second = parse.getField(DateField.SECOND);
        String cron = second + " " + minute + " " + hour + " * * ? ";

        dutySchedule.setCron(cron);

        int insert = dutyScheduleMapper.insert(dutySchedule);
        return ResultUtil.execOp(insert, "插入");
    }

    @Override
    public ResultBean update(DutySchedule dutySchedule) {
        String signout = dutySchedule.getSignout();
        DateTime parse = DateUtil.parse(signout);

        int hour = parse.getField(DateField.HOUR_OF_DAY);
        int minute = parse.getField(DateField.MINUTE);
        int second = parse.getField(DateField.SECOND);
        String cron = second + " " + minute + " " + hour + " * * ? ";

        dutySchedule.setCron(cron);

        int update = dutyScheduleMapper.updateById(dutySchedule);
        return ResultUtil.execOp(update, "修改");
    }

    @Override
    public ResultBean delete(DutySchedule dutySchedule) {
        int i = dutyScheduleMapper.deleteById(dutySchedule);
        return ResultUtil.execOp(i, "删除");
    }

    @Override
    public DutySchedule findOne(int modeCode, int shiftop) {
        List<Integer> seasons = new ArrayList<Integer>();
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        if (month == 5 || month == 6 || month == 7 || month == 8 || month == 9) {
            seasons.add(1);
            seasons.add(0);
        } else { //冬季出勤
            //冬季勤务模式
            seasons.add(2);
            seasons.add(0);
        }
        DutySchedule dutySchedule = dutyScheduleMapper.selectOne(Wrappers.<DutySchedule>lambdaQuery()
                .eq(DutySchedule::getModeCode, modeCode)
                .eq(DutySchedule::getShiftop, shiftop).in(DutySchedule::getSeason, seasons));
        return dutySchedule;
    }

    @Override
    public List<DutySchedule> findOneByModeCode(Integer modeCode) {
        List<DutySchedule> dutySchedules = dutyScheduleMapper.selectList(Wrappers.<DutySchedule>lambdaQuery()
                .eq(DutySchedule::getModeCode, modeCode));
        return dutySchedules;
    }

    @Resource
    XareaModeMapper xareaModeMapper;

    /**
     * 根据modeCode 查询详情
     *
     * @param mode modeCode
     * @return ResultBean
     */
    @Override
    public ResultBean findMode(String mode) {

        List<DutySchedule> dutySchedules = dutyScheduleMapper.selectList(Wrappers.<DutySchedule>lambdaQuery()
                .eq(DutySchedule::getModeCode, mode));
        return ResultUtil.setOK("查询", dutySchedules);

    }

}
